# -*- coding: utf-8 -*-
#from nltk.stem.snowball import SnowballStemmer
from operator import itemgetter
import re
import os
import codecs
import sys
import dataset

class CustomClassifier:

	def __init__(self, workspace,categories):
		#sys.setDefaultEncoding('utf-8')
		#self.stemmer = SnowballStemmer("spanish") # Choose a language
		self.dataDirectory = workspace  + os.sep + "data" + os.sep		
		self.categories = categories

		self.readStopWords('spanishSW.txt')
		self.wordSet = {}

		self.db = dataset.DatasetDB(workspace + os.sep + "customClassifier",self.categories)
		if self.db.isEmpty():
			self.makeStructure()
		else:
			print "Ya existe el dataset.db y NO es vacio"
			self.wordSet = self.db.loadStructure()

	def readStopWords(self,file):
		self.stopWords = []
		file = codecs.open(file,"r",'utf-8')
		text = file.read()
		self.stopWords = (self.remove_extra_spaces(text)).split(' ')

	def remove_extra_spaces(self,data):
		p = re.compile(r'\s+')
		return p.sub(' ', data)

	def remove_symbols(self,data):
		p = re.compile(r'[\!@#$%&*()-_+="]')
		return p.sub('',data)

	def categoryMap(self):
		current = {}
		for c in self.categories:
			current[c] = 0

		return current
		
	def makeStructure(self):		
		for category in self.categories:
			wordsCountByCategory = 0.0
			files = os.listdir(self.dataDirectory + category) #rescato el contenido del directorio actual.
			for nameFile in files:
				file = codecs.open(self.dataDirectory + category + "/" + nameFile,"r","utf-8")
				text = file.read()
				text = text.lower()
				words = (self.remove_extra_spaces(text)).split(' ')
				for w in words:
					w = self.remove_symbols(w)
					if ((w in self.stopWords) == False):
						#w = self.stemmer.stem(w)
						if not (w in self.wordSet):					
							self.wordSet[unicode(w)] = self.categoryMap()

						self.wordSet[unicode(w)][category] = self.wordSet[w][category] + 1
						wordsCountByCategory = wordsCountByCategory + 1

			print "Category: " + category
			for w in self.wordSet:
				self.wordSet[unicode(w)][category] = self.wordSet[unicode(w)][category] / wordsCountByCategory
				self.db.addWord(unicode(w), category, self.wordSet[unicode(w)][category])

	def classify(self, text):
		classification = self.categoryMap()
		text = unicode(text.lower())
		words = (self.remove_extra_spaces(text)).split(' ')
		for w in words:
			w = self.remove_symbols(w)
			if ((w in self.stopWords) == False):
				if (w in self.wordSet):				
					for category in self.categories:
						classification[category] = classification[category] + self.wordSet[unicode(w)][category]
				
		maxCategoryValue = 0
		suitableCategory = ""
		for category in self.categories:
			if classification[category] > maxCategoryValue:
				maxCategoryValue = classification[category]
				suitableCategory = category		

		return suitableCategory